<template>
  <div class="sys-teacher-list">
    <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
      <div class="widget am-cf">
        <div class="widget-head am-cf">
          <div class="widget-title  am-cf">老师列表</div>
        </div>
        <div class="widget-body  am-fr">

          <div class="am-u-sm-12 am-u-md-6 am-u-lg-4">
            <div class="am-form-group">
              <div class="am-btn-toolbar">
                <div class="am-btn-group am-btn-group-xs">
                  <button type="button" class="am-btn am-btn-default am-btn-success"
                          @click="$router.push('/main/sys/teacher/add')" v-if="hasPermission('add')"><span
                    class="am-icon-plus"></span>新增
                  </button>
                </div>
              </div>
            </div>
          </div>
          <div class="am-u-sm-12 am-u-md-6 am-u-lg-2">
            <div class="am-form-group tpl-table-list-select">
              <selected v-model="searchConfig.searchItem">
                <select data-am-selected="{btnSize: 'sm'}" placeholder="搜索选项">
                  <option></option>
                  <option value="teacherName">姓名</option>
                  <!--<option value="idNo">身份证号码</option>-->
                  <option value="teachGradeNames">任教年级</option>
                  <option value="teachSubjectNames">任教科目</option>
                  <option value="areaTeamName">区域</option>
                  <option value="phoneNo">手机号码</option>
                  <option value="busTeamName">业务组</option>
                </select>
              </selected>
            </div>
          </div>
          <div class="am-u-sm-12 am-u-md-12 am-u-lg-2">
            <div class="am-input-group-sm tpl-form-border-form cl-p">
              <input type="text" class="am-form-field " v-model="searchConfig.searchValue">
            </div>
          </div>
          <div class="am-u-sm-12 am-u-md-12 am-u-lg-2">
            <el-select class="am-fr" placeholder="选择职能" v-model="searchConfig.accessType">
              <el-option label="全选" value=""></el-option>
              <el-option label="任课老师" value="0"></el-option>
              <el-option label="班主任" value="1"></el-option>
              <el-option label="外部班主任" value="2"></el-option>
            </el-select>
          </div>
          <button type="button" class="am-btn am-btn-default am-btn-success button-search"
                  @click="search" ><span class="am-icon-search"></span>查询
          </button>
          <button type="button" class="am-btn am-btn-default am-btn-success button-export"
                  @click="exportTeachers" ><span class="am-icon-download"></span>导出
          </button>

          <div class="am-u-sm-12 am-scrollable-horizontal">
            <el-table
              :data="tableData"
              border
              stripe
              style="min-width: 100%">
              <el-table-column
                fixed
                prop="teacherName"
                label="姓名"
                min-width="100">
              </el-table-column>
              <el-table-column
                label="职能"
                min-width="100">
                <template scope="scope">
                  {{{'0':'任课老师', '1': '班主任' ,'2':'外部班主任'}[scope.row.accessType]}}
                </template>
              </el-table-column>
              <el-table-column
                label="星级"
                min-width="100">
                <template scope="scope">
                  {{scope.row.level}}星
                </template>
              </el-table-column>
              <el-table-column
                label="性别"
                min-width="100">
                <template scope="scope">
                  {{scope.row.sex == 1 ? '男' : '女' }}
                </template>
              </el-table-column>
              <el-table-column
                prop="phoneNo"
                label="电话号码"
                min-width="150">
              </el-table-column>
              <el-table-column
                prop="teachGradeNames"
                label="任教年级"
                min-width="200">
                <template scope="scope">
                  {{scope.row.teachGradeNames|| ''}}
                </template>
              </el-table-column>
              <el-table-column
                prop="teachSubjectNames"
                label="任教科目"
                min-width="100">
              </el-table-column>
              <el-table-column
                prop="areaTeamName"
                label="所在区域"
                min-width="100">
              </el-table-column>
              <el-table-column
                prop="busTeamName"
                label="人事关系"
                min-width="160">
              </el-table-column>
              <el-table-column
                label="入职时间"
                min-width="100">
                <template scope="scope">
                  {{scope.row.joinTime | formatDate}}
                </template>
              </el-table-column>
              <el-table-column
                label="任职性质"
                min-width="100">
                <template scope="scope">
                  <div>
                    {{scope.row.jobNature | getJobNature}}
                  </div>
                </template>
              </el-table-column>
              <el-table-column
                label="状态"
                min-width="100">
                <template scope="scope">
                  {{scope.row.status == 0 ? '离职' : '在职'}}

                </template>
              </el-table-column>
              <el-table-column
                fixed="right"
                label="操作"
                width="120">
                <template scope="scope">
                  <el-dropdown>
                    <span class="el-dropdown-link">
                      操作菜单<i class="el-icon-caret-bottom el-icon--right"></i>
                    </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item v-if="hasPermission('edit')" @click.native="$router.push('/main/sys/teacher/edit/'+scope.row.teacherId)">编辑
                      </el-dropdown-item>
                      <el-dropdown-item   v-if="hasPermission('edit')"  @click.native="delaySetting(scope.row)">预设</el-dropdown-item>

                      <el-dropdown-item v-if="hasPermission('del')" @click.native="del(scope.row.teacherId)">删除</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <div class="am-u-lg-12 am-cf">

            <div class="am-fr">
              <pagination v-bind:total="total" v-bind:pageNo="pageNo" v-bind:pageSize="pageSize"
                          @paging="loadTableData"/>
            </div>
          </div>
        </div>
      </div>
    </div>
    <change-teacher-message ref="changeTeacherMessage"></change-teacher-message>
  </div>

</template>

<script>
  import io from '../../lib/io'

  import Pagination from '../base/Pagination'
  import ChangeTeacherMessage from './changeTeacherMessage.vue'


  export default{
    data: function () {
      return {
        tableData: [],
        total: 0,
        pageSize: 10,
        pageNo: 1,
        query: {},
        searchConfig: {
          accessType:"",
        },
      }
    },
    components: {
      Pagination,
      'change-teacher-message':ChangeTeacherMessage,
    },
    mounted: function () {
      $(window).smoothScroll()
    },
    created: function () {
      this.loadTableData(this.pageNo);
    },
    methods: {

      delaySetting: function (teacher) {
        this.$refs.changeTeacherMessage.teacher=teacher
        this.$refs.changeTeacherMessage.show()
      },
      del: function (teacherId) {
        const _this = this;
        _this.$confirm('你确定要删除？',
          function () {
            io.post(io.apiAdminDelTeacher, {teacherId}, function (ret) {
              if (ret.success) {
                _this.$toast('OK')
                _this.loadTableData()
              } else {
                _this.$alert(ret.desc)
              }
            })
          });
      },
      search: function () {
        this.query = {}
        if(this.searchConfig.searchItem) {
          this.query[this.searchConfig.searchItem] = this.searchConfig.searchValue || ''
        }
        this.query.accessType = this.searchConfig.accessType || ''
        this.loadTableData(1)
      },
      //导出教师信息
      exportTeachers:function(){
        this.query = {}
        if(this.searchConfig.searchItem) {
          this.query[this.searchConfig.searchItem] = this.searchConfig.searchValue || ''
        }
        this.query.accessType = this.searchConfig.accessType || ''
        var url = io.apiAdminExportTeachers + '?accessToken=' + io.getHeaders().accessToken;

        for(var i in this.query)    {
           url = url + '&' + i + "="+ this.query[i];
        }
        window.open(url)
      },
      loadTableData: function (pageNo) {
        var _this = this
        _this.pageNo = pageNo || _this.pageNo || 1
        io.post(io.apiAdminTeacherList, $.extend({
          pageNo: _this.pageNo,
          pageSize: _this.pageSize
        }, _this.query), function (ret) {
          if (ret.success) {
            _this.total = ret.data.total
            _this.tableData = ret.data.list
          } else {
            _this.$alert(ret.desc)
          }
        })
      }
    }
  }
</script>

<style lang="less">
  .sys-teacher-list {
    .button-export{
      font-size: 14px;
    }
    .button-search{
      font-size: 14px;
    }
    .am-selected {
      width: 100%;
    }
  }
</style>
